﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace Chaos.network
{
    class detectionserver
    {
        public static int Port { get { return 48928; } }
        udpconnection m_receiveconnection;

        public detectionserver()
		{
		}

        public void start(int in_port)
        {
            UdpClient udpreceiveclient = new UdpClient(in_port);
            udpreceiveclient.EnableBroadcast = true;
            IPEndPoint ipendpoint = new IPEndPoint(IPAddress.Any, 0);
            m_receiveconnection = new udpconnection(udpreceiveclient, ipendpoint);
            m_receiveconnection.start();
            m_receiveconnection.OnNetworkData += new udpconnection.OnNetworkDataDelegate(Connection_OnNetworkData);
        }

        public void abort()
        {
            m_receiveconnection.abort();
        }

        public void shutdown()
        {
            abort();
        }

        void Connection_OnNetworkData(object sender, udpconnection.OnNetworkDataEvent e)
        {
            messages.message.context c = new messages.message.context();
            c.Connection = sender as connection;
            c.IPEndPoint = e.IPEndPoint;
            c.IPEndPoint.Port = detectionclient.Port;
            e.Message.process(c);
        }

    }
}
